<?php
/**
 * @title 1124. 表现良好的最长时间段 - 循环
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $hours
     * @return Integer
     */
    function longestWPI($hours) {
        /**
         * @since 2020-07-28 left=疲劳, right=不疲劳
         */
        $left = $right = 0;

        /**
         * @since 2020-07-28 遍历
         */
        $max = 0;
        for($i=0, $len=count($hours); $i<$len; $i++){
            if($hours[$i] > 8){
                $left++;
            } else {
                $right++;

                /**
                 * @since 2020-07-28 不疲劳>=疲劳, 重新初始化
                 */
                if($right >= $left){
                    $max = max($max, $left+$right-1);
                    $left = $right = 0;
                } else {}
            }
        }

        /**
         * @since 2020-07-28 收官
         */
        $max = max($max, $left+$right);

        /**
         * @return
         */
        return $max;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28_3.php
 */
$datas = [
    // [9,9,6,0,6,6,9],
    // [9,9,6,9,6],
    [6,9,9],
    [6,9,9,9,6],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->longestWPI($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
